home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Skunkware 5
/
Skunkware 5.iso
/
man
/
cat.1
/
perltrap.1
< prev
next >
Wrap
Text File
|
1995-07-25
|
17KB
|
463 lines
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
NNNNAAAAMMMMEEEE
perltrap - Perl traps for the unwary
DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
The biggest trap of all is forgetting to use the ----wwww switch;
see the _p_e_r_l_r_u_n manpage. Making your entire program
runnable under
use strict;
can help make your program more bullet-proof, but sometimes
it's too annoying for quick throw-away programs.
AAAAwwwwkkkk TTTTrrrraaaappppssss
Accustomed aaaawwwwkkkk users should take special note of the
following:
o+ The English module, loaded via
use English;
allows you to refer to special variables (like $RS) as
though they were in aaaawwwwkkkk; see the _p_e_r_l_v_a_r manpage for
details.
o+ Semicolons are required after all simple statements in
Perl (except at the end of a block). Newline is not a
statement delimiter.
o+ Curly brackets are required on ifs and whiles.
o+ Variables begin with "$" or "@" in Perl.
o+ Arrays index from 0. Likewise string positions in
_s_u_b_s_t_r() and _i_n_d_e_x().
o+ You have to decide whether your array has numeric or
string indices.
o+ Associative array values do not spring into existence
upon mere reference.
o+ You have to decide whether you want to use string or
numeric comparisons.
o+ Reading an input line does not split it for you. You
get to split it yourself to an array. And _s_p_l_i_t()
operator has different arguments.
o+ The current input line is normally in $_, not $0. It
generally does not have the newline stripped. ($0 is
Page 1 (printed 6/30/95)
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
the name of the program executed.) See the _p_e_r_l_v_a_r
manpage.
o+ $<_d_i_g_i_t> does not refer to fields--it refers to
substrings matched by the last match pattern.
o+ The _p_r_i_n_t() statement does not add field and record
separators unless you set $, and $.. You can set $OFS
and $ORS if you're using the English module.
o+ You must open your files before you print to them.
o+ The range operator is "..", not comma. The comma
operator works as in C.
o+ The match operator is "=~", not "~". ("~" is the one's
complement operator, as in C.)
o+ The exponentiation operator is "**", not "^". "^" is
the XOR operator, as in C. (You know, one could get the
feeling that aaaawwwwkkkk is basically incompatible with C.)
o+ The concatenation operator is ".", not the null string.
(Using the null string would render /pat/ /pat/
unparsable, since the third slash would be interpreted
as a division operator--the tokener is in fact slightly
context sensitive for operators like "/", "?", and ">".
And in fact, "." itself can be the beginning of a
number.)
o+ The next, exit, and continue keywords work differently.
o+ The following variables work differently:
Awk Perl
ARGC $#ARGV or scalar @ARGV
ARGV[0] $0
FILENAME $ARGV
FNR $. - something
FS (whatever you like)
NF $#Fld, or some such
NR $.
OFMT $#
OFS $,
ORS $\
RLENGTH length($&)
RS $/
RSTART length($`)
SUBSEP $;
Page 2 (printed 6/30/95)
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
o+ You cannot set $RS to a pattern, only a string.
o+ When in doubt, run the aaaawwwwkkkk construct through aaaa2222pppp and see
what it gives you.
CCCC TTTTrrrraaaappppssss
Cerebral C programmers should take note of the following:
o+ Curly brackets are required on if's and while's.
o+ You must use elsif rather than else if.
o+ The break and continue keywords from C become in Perl
last and next, respectively. Unlike in C, these do _N_O_T
work within a do { } while construct.
o+ There's no switch statement. (But it's easy to build
one on the fly.)
o+ Variables begin with "$" or "@" in Perl.
o+ _p_r_i_n_t_f() does not implement the "*" format for
interpolating field widths, but it's trivial to use
interpolation of double-quoted strings to achieve the
same effect.
o+ Comments begin with "#", not "/*".
o+ You can't take the address of anything, although a
similar operator in Perl 5 is the backslash, which
creates a reference.
o+ ARGV must be capitalized.
o+ System calls such as _l_i_n_k(), _u_n_l_i_n_k(), _r_e_n_a_m_e(), etc.
return nonzero for success, not 0.
o+ Signal handlers deal with signal names, not numbers.
Use kill -l to find their names on your system.
SSSSeeeedddd TTTTrrrraaaappppssss
Seasoned sssseeeedddd programmers should take note of the following:
o+ Backreferences in substitutions use "$" rather than "\".
o+ The pattern matching metacharacters "(", ")", and "|" do
not have backslashes in front.
o+ The range operator is ..., rather than comma.
Page 3 (printed 6/30/95)
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
SSSShhhheeeellllllll TTTTrrrraaaappppssss
Sharp shell programmers should take note of the following:
o+ The backtick operator does variable interpretation
without regard to the presence of single quotes in the
command.
o+ The backtick operator does no translation of the return
value, unlike ccccsssshhhh.
o+ Shells (especially ccccsssshhhh) do several levels of
substitution on each command line. Perl does
substitution only in certain constructs such as double
quotes, backticks, angle brackets, and search patterns.
o+ Shells interpret scripts a little bit at a time. Perl
compiles the entire program before executing it (except
for BEGIN blocks, which execute at compile time).
o+ The arguments are available via @ARGV, not $1, $2, etc.
o+ The environment is not automatically made available as
separate scalar variables.
PPPPeeeerrrrllll TTTTrrrraaaappppssss
Practicing Perl Programmers should take note of the
following:
o+ Remember that many operations behave differently in a
list context than they do in a scalar one. See the
_p_e_r_l_d_a_t_a manpage for details.
o+ Avoid barewords if you can, especially all lower-case
ones. You can't tell just by looking at it whether a
bareword is a function or a string. By using quotes on
strings and parens on function calls, you won't ever get
them confused.
o+ You cannot discern from mere inspection which built-ins
are unary operators (like _c_h_o_p() and _c_h_d_i_r()) and which
are list operators (like _p_r_i_n_t() and _u_n_l_i_n_k()). (User-
defined subroutines can oooonnnnllllyyyy be list operators, never
unary ones.) See the _p_e_r_l_o_p manpage.
o+ People have a hard type remembering that some functions
default to $_, or @ARGV, or whatever, but that others
which you might expect to do not.
o+ Remember not to use "=" when you need "=~"; these two
constructs are quite different:
Page 4 (printed 6/30/95)
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
$x = /foo/;
$x =~ /foo/;
o+ The do {} construct isn't a real loop that you can use
loop control on.
o+ Use _m_y() for local variables whenever you can get away
with it (but see the _p_e_r_l_f_o_r_m manpage for where you
can't). Using _l_o_c_a_l() actually gives a local value to a
global variable, which leaves you open to unforeseen
side-effects of dynamic scoping.
PPPPeeeerrrrllll4444 TTTTrrrraaaappppssss
Penitent Perl 4 Programmers should take note of the
following incompatible changes that occurred between release
4 and release 5:
o+ @ now always interpolates an array in double-quotish
strings. Some programs may now need to use backslash to
protect any @ that shouldn't interpolate.
like subroutine calls if a subroutine by that name is defined
before the compiler sees them. For example:
o+ Barewords that used to look like strings to Perl will now look
sub SeeYa { die "Hasta la vista, baby!" }
$SIG{QUIT} = SeeYa;
In Perl 4, that set the signal handler; in Perl 5, it
actually calls the function! You may use the ----wwww switch
to find such places.
o+ Symbols starting with _ are no longer forced into
package main, except for $_ itself (and @_, etc.).
o+ s'$lhs'$rhs' now does no interpolation on either side.
It used to interpolate $lhs but not $rhs.
o+ The second and third arguments of _s_p_l_i_c_e() are now
evaluated in scalar context (as the book says) rather
than list context.
o+ These are now semantic errors because of precedence:
shift @list + 20;
$n = keys %map + 20;
Because if that were to work, then this couldn't:
sleep $dormancy + 20;
Page 5 (printed 6/30/95)
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
o+ open FOO || die is now incorrect. You need parens
around the filehandle. While temporarily supported,
using such a construct will generate a non-fatal (but
non-suppressible) warning.
o+ The elements of argument lists for formats are now
evaluated in list context. This means you can
interpolate list values now.
o+ You can't do a goto into a block that is optimized away.
Darn.
o+ It is no longer syntactically legal to use whitespace as
the name of a variable, or as a delimiter for any kind
of quote construct. Double darn.
o+ The _c_a_l_l_e_r() function now returns a false value in a
scalar context if there is no caller. This lets library
files determine if they're being required.
o+ m//g now attaches its state to the searched string
rather than the regular expression.
o+ reverse is no longer allowed as the name of a sort
subroutine.
o+ ttttaaaaiiiinnnnttttppppeeeerrrrllll is no longer a separate executable. There is
now a ----TTTT switch to turn on tainting when it isn't turned
on automatically.
o+ Double-quoted strings may no longer end with an
unescaped $ or @.
o+ The archaic while/if BLOCK BLOCK syntax is no longer
supported.
o+ Negative array subscripts now count from the end of the
array.
o+ The comma operator in a scalar context is now guaranteed
to give a scalar context to its arguments.
o+ The ** operator now binds more tightly than unary minus.
It was documented to work this way before, but didn't.
o+ Setting $#array lower now discards array elements.
o+ _d_e_l_e_t_e() is not guaranteed to return the old value for
_t_i_e()d arrays, since this capability may be onerous for
some modules to implement.
Page 6 (printed 6/30/95)
PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
o+ Some error messages will be different.
o+ Some bugs may have been inadvertently removed.
Page 7 (printed 6/30/95)